package com.xiaoshuidi.cloud.module.system.controller.app.oss;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.operatelog.core.annotations.OperateLog;
import com.xiaoshuidi.cloud.module.system.service.oos.OosService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.util.Map;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name = "租客 APP - oss")
@RestController("appOssController")
@RequestMapping("/system/oss")
@Validated
@Slf4j
public class OssController {

    @Resource
    private OosService oosService;

    @PostMapping("/upload")
    @PermitAll
    @Operation(summary = "oss上传文件")
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public CommonResult<Map<String, Object>> upload(@RequestPart("file") MultipartFile file) {
        return success(oosService.uploadFile(file, file.getOriginalFilename()));
    }


    @GetMapping("/generate-presigned-url")
    @PermitAll
    @Operation(summary = "oss获取分享链接")
    public CommonResult<String> generatePresignedUrl(String fileKey){
        return success(oosService.generatePresignedUrl(fileKey));
    }


}
